#ifndef _CHARARRAY_H_CSEAPI_
#define _CHARARRAY_H_CSEAPI_

class TIntObj;
class TObjArray;

class CSE_IMPEXP_IN TCharArray
{
private:
    TBool copyFrom(const TXchar* pSour, int iLen, int iOff) const;
    TCharArray& joinEx(const TXchar* pSour, int iSourLen);
    void insertEx(int iPos, const TXchar *pSour, int iLen);

protected:
    TXchar *m_data;

public:
    TCharArray();
    TCharArray(int MagicNum, TCharArray& str);
    
    TCharArray(const TCharArray& s);
    TCharArray(TXchar ch, int count=1);
    TCharArray(const TXchar* pSour, int n=-1, int iExt=0);
    ~TCharArray();
    
    TCharArray& join(const TCharArray& str, int index=0);
    TCharArray& join(const TXchar* pSour, int iSourLen=-1);

    TCharArray& operator=(const TCharArray& str);
    TCharArray& operator=(const TXchar* pSour);
    TCharArray& operator=(TXchar ch);

    void operator+=(const TCharArray& str);
    void operator+=(const TXchar* pSour);
    void operator+=(TXchar ch);
    TCharArray multi(int n) const;
    
    TXchar& sub(int index) const;         // same to operator[]
    TXchar& operator[](int index) const;
    operator TXchar*() const;
    
    TXchar *addr() const;
    int len() const;
    TBool isEmpty() const;
    TInt compare(const TCharArray& sOther) const;
    TInt compare(const TXchar *pOther) const;

    TCharArray slice(int index, int iLen=-1) const;

    TXchar *transfer(TXchar *pOther);
    void exchange(TCharArray& other);
    TXchar append(TXchar ch);
    TCharArray& add(TXchar ch, int *pIndex=NULL);
    const TIntObj& insert(int index, const TIntObj& obj);
    void insertSlice(int index, const TXchar *pSour, int iLen=-1);
    void remove(int index, int iLen=1);

    void clear();
    int truncate(int iLen) const;
    int sortAdd(TXchar ch);
    void sort() const;

    static TCharArray format(const TXchar *frmt, ...);

    void resize(int iSize);
    TUint32 hash() const;
    TCharArray str(TXchar pre=_T('"')) const;

    TBool toInt(TInt& ret) const;
    TBool toWInt(TInt64& ret) const;
    TBool toDouble(TDouble& ret) const;

    const TCharArray& upper() const;
    const TCharArray& lower() const;
    int trim() const;
    int trimLeft() const;
    int trimRight() const;
    int find(TXchar ch, int from=0) const;
    int find(const TXchar *sSub, int from=0) const;
    
    TBool enumItem(void*& pIter, TXchar *pRet=NULL) const;
    
    int count(const TXchar* sSub, int iStart=0, int iEnd=0) const;
    TCharArray& align(int iWidth, TXchar fillChar = _T(' '));
    TCharArray& alignLeft(int iWidth, TXchar fillChar = _T(' '));
    TCharArray& alignRight(int iWidth, TXchar fillChar = _T(' '));
    TCharArray& replace(const TXchar *sSub, const TXchar *sNew, int count=-1);
    
    TObjArray split(const TXchar* sSep=NULL, int count=-1) const;
    TObjArray splitLines(TBool keepEnds=false, int count=-1) const;
};

extern CSE_IMPEXP_IN const TCharArray& CseEmptyStr;

extern CSE_IMPEXP_IN TCharArray Cse_StrFromAnsi(const char *sour, int n=-1);
extern CSE_IMPEXP_IN TCharArray Cse_StrFromUnicode(const TWchar *sour, int n=-1);

extern CSE_IMPEXP_IN TCharArray operator+(const TCharArray& x, const TCharArray& y);
extern CSE_IMPEXP_IN TCharArray operator+(const TCharArray& x, const TXchar *y);
extern CSE_IMPEXP_IN TCharArray operator+(const TCharArray& x, TXchar y);
extern CSE_IMPEXP_IN TCharArray operator+(TXchar x, const TCharArray& y);
extern CSE_IMPEXP_IN TCharArray operator+(const TXchar *x, const TCharArray& y);

extern CSE_IMPEXP_IN TBool operator==(const TCharArray& x, const TCharArray& y);
extern CSE_IMPEXP_IN TBool operator==(const TCharArray& x, const TXchar *y);
extern CSE_IMPEXP_IN TBool operator==(const TXchar *x, const TCharArray& y);

extern CSE_IMPEXP_IN TBool operator!=(const TCharArray& x, const TCharArray& y);
extern CSE_IMPEXP_IN TBool operator!=(const TCharArray& x, const TXchar *y);
extern CSE_IMPEXP_IN TBool operator!=(const TXchar *x, const TCharArray& y);

extern CSE_IMPEXP_IN TBool operator>(const TCharArray& x, const TCharArray& y);
extern CSE_IMPEXP_IN TBool operator>(const TCharArray& x, const TXchar *y);
extern CSE_IMPEXP_IN TBool operator>(const TXchar *x, const TCharArray& y);

extern CSE_IMPEXP_IN TBool operator>=(const TCharArray& x, const TCharArray& y);
extern CSE_IMPEXP_IN TBool operator>=(const TCharArray& x, const TXchar *y);
extern CSE_IMPEXP_IN TBool operator>=(const TXchar *x, const TCharArray& y);

extern CSE_IMPEXP_IN TBool operator<(const TCharArray& x, const TCharArray& y);
extern CSE_IMPEXP_IN TBool operator<(const TCharArray& x, const TXchar *y);
extern CSE_IMPEXP_IN TBool operator<(const TXchar *x, const TCharArray& y);

extern CSE_IMPEXP_IN TBool operator<=(const TCharArray& x, const TCharArray& y);
extern CSE_IMPEXP_IN TBool operator<=(const TCharArray& x, const TXchar *y);
extern CSE_IMPEXP_IN TBool operator<=(const TXchar *x, const TCharArray& y);

#endif
